/*
 * CreateActorFrame.java
 *
 * Created on 11/11/2011, 07:47:23 PM
 */
package presentationLayer.frames;

import java.util.Hashtable;
import java.util.Vector;

import logicLayer.schedulingAlgorithmSystem.FCFS;
import logicLayer.schedulingAlgorithmSystem.PrioritiesSA;
import logicLayer.schedulingAlgorithmSystem.SchedulingAlgorithm;
import logicLayer.schedulingSystem.Actor;
import logicLayer.schedulingSystem.Resource;
import logicLayer.schedulingSystem.SchedulingSystem;
import logicLayer.schedulingSystem.Updater;

/**
 * 
 * @author F.Rossi
 */
// CHECKEAR SCHEDULING SYSTEM EN ENTRADA, FUNCION EN DESARROLLO Y EN CERRAR
// VENTANA CON CREACION
// COMO HAGO PARA MODIFICAR SIEMPRE EL MISMO MAIN FRAME??
public class CreateActorFrame extends javax.swing.JFrame {

	public static CreateActorFrame getInstance() {
		if (CREATEACTORFRAME_INSTANCE == null) {
			CREATEACTORFRAME_INSTANCE = new CreateActorFrame();
		}
		return CREATEACTORFRAME_INSTANCE;
	}

	public static boolean isNum(String s) {
		try {
			Integer.parseInt(s);
		} catch (NumberFormatException nfe) {
			return false;
		}
		return true;
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger
					.getLogger(CreateActorFrame.class.getName()).log(
							java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger
					.getLogger(CreateActorFrame.class.getName()).log(
							java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger
					.getLogger(CreateActorFrame.class.getName()).log(
							java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger
					.getLogger(CreateActorFrame.class.getName()).log(
							java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
			}
		});
	}

	private Actor newActor;

	private SchedulingSystem currentSchedulingSystem;

	private Vector<Actor> actorsList;

	private Vector<Resource> resourcesMainList;

	private Vector<Resource> resourcesList;

	private Updater updater;

	private static CreateActorFrame CREATEACTORFRAME_INSTANCE;

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JRadioButton jRadioButton1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JTextField jTextField3;
	private javax.swing.JTextField jTextField4;

	// End of variables declaration//GEN-END:variables
	/**
	 * Creates new form CreateActorFrame
	 */
	private CreateActorFrame() {
		initComponents();
		this.jButton3.setEnabled(false);
		this.setResizable(false);
	}

	private boolean areValidFields() {
		if (!isNum(this.jTextField1.getText()))// Capacity
			return false;
		if (!isNum(this.jTextField2.getText()))// MaxTasks
			return false;
		if (!isNum(this.jTextField3.getText()))// Quantum
			return false;
		if (!isNum(this.jTextField4.getText()))// MaxRelations
			return false;

		return true;
	}

	private void clearTable() {
		for (int i = 0; i < this.jTable1.getRowCount(); i++)
			for (int j = 0; j < this.jTable1.getColumnCount(); j++) {
				this.jTable1.setValueAt("", i, j);
			}
	}

	public Vector<Actor> getActorsList() {
		return actorsList;
	}

	public Vector<Resource> getMainResourcesList() {
		return resourcesMainList;
	}

	public Vector<Resource> getResourcesList() {
		return resourcesList;
	}

	public SchedulingSystem getSchedulingSystem() {
		return this.currentSchedulingSystem;
	}

	public Updater getUpdater() {
		return this.updater;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jPanel2 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jTextField1 = new javax.swing.JTextField();
		jTextField2 = new javax.swing.JTextField();
		jComboBox1 = new javax.swing.JComboBox();
		jTextField3 = new javax.swing.JTextField();
		jLabel5 = new javax.swing.JLabel();
		jTextField4 = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		jButton3 = new javax.swing.JButton();
		jRadioButton1 = new javax.swing.JRadioButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Create Actor",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION,
				new java.awt.Font("Verdana", 1, 11))); // NOI18N

		jLabel1.setText("Capacity");

		jLabel2.setText("Maximum number of tasks");

		jLabel3.setText("Scheduling Algorithm");

		jLabel4.setText("Quantum (Value: greater than 2)");

		jPanel2.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Properties"));

		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null }, { null, null },
						{ null, null }, { null, null }, { null, null },
						{ null, null }, { null, null }, { null, null },
						{ null, null }, { null, null }, { null, null },
						{ null, null }, { null, null }, { null, null },
						{ null, null }, { null, null }, { null, null },
						{ null, null }, { null, null }, { null, null } },
				new String[] { "Key", "Value" }));
		jScrollPane1.setViewportView(jTable1);

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel2Layout
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(jScrollPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 375,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(17, Short.MAX_VALUE)));
		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel2Layout
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(jScrollPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 92,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		jButton1.setText("Create");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setText("Cancel");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"First come first server", "Prioridades" }));

		jTextField3.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jTextField3ActionPerformed(evt);
			}
		});

		jLabel5.setText("Maximum number of relations");

		jLabel6.setText("Updater");

		jButton3.setText("Edit");
		jButton3.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});

		jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jRadioButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jPanel2,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel6)
																		.addGap(18,
																				18,
																				18)
																		.addComponent(
																				jRadioButton1)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				285,
																				Short.MAX_VALUE)
																		.addComponent(
																				jButton3))
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jLabel1)
																						.addComponent(
																								jLabel2)
																						.addComponent(
																								jLabel3)
																						.addComponent(
																								jLabel4)
																						.addComponent(
																								jLabel5))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				125,
																				Short.MAX_VALUE)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								jTextField4)
																						.addComponent(
																								jTextField3)
																						.addComponent(
																								jComboBox1,
																								0,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jTextField2)
																						.addComponent(
																								jTextField1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								131,
																								Short.MAX_VALUE)))
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				jButton1)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButton2)))
										.addContainerGap()));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																jTextField1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																jTextField2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel3)
														.addComponent(
																jComboBox1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jTextField3,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel4))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel5)
														.addComponent(
																jTextField4,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jButton3)
														.addComponent(jLabel6)
														.addComponent(
																jRadioButton1))
										.addGap(18, 18, 18)
										.addComponent(
												jPanel2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jButton2)
														.addComponent(jButton1))
										.addGap(134, 134, 134)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jPanel1,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addComponent(jPanel1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 383,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	/*
	 * Action of 'Create' button, which creates a new Actor taking the values of
	 * the screen components. Updating Actors list too, and closing the current
	 * panel.
	 */
	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		if (this.areValidFields()) {
			if (this.newActor == null)
				this.newActor = new Actor("actor"
						+ String.valueOf(this.getActorsList().size()), "actor",
						null, 80, null, 0, 0, null, 0, null, null);

			// Get selected algorithm
			SchedulingAlgorithm saReadyList;
			if (this.jComboBox1.getSelectedItem().equals("Prioridades")) {
				saReadyList = new PrioritiesSA();
			} else {
				saReadyList = new FCFS();
			}

			/* Get Properties */
			Hashtable propertiesTable = new Hashtable();

			for (int i = 0; i < this.jTable1.getRowCount(); i++) {
				if ((this.jTable1.getValueAt(i, 0) != null)
						&& (this.jTable1.getValueAt(i, 1) != null)
						&& (!this.jTable1.getValueAt(i, 0).equals(""))
						&& (!this.jTable1.getValueAt(i, 1).equals(""))) {
					propertiesTable.put(this.jTable1.getValueAt(i, 0)
							.toString(), this.jTable1.getValueAt(i, 1)
							.toString());
				} else {
					i = this.jTable1.getRowCount();
				}
			}

			this.newActor.setSaReadyList(saReadyList);
			this.newActor.setLimitTime(Integer.parseInt(this.jTextField3
					.getText()));
			this.newActor.setSchedulingSystem(this.getSchedulingSystem());
			this.newActor.setCapacity(Integer.parseInt(this.jTextField1
					.getText()));
			this.newActor.setMaxTasksNumber(Integer.parseInt(this.jTextField2
					.getText()));
			this.newActor.setProperties(propertiesTable);
			this.newActor.setResourceMaxLimit(Integer.parseInt(this.jTextField4
					.getText()));
			this.newActor.setUpdater(this.getUpdater());
			this.getActorsList().add(this.newActor);
			SimulatorFrame.getInstance().setActorsList(this.getActorsList());
			this.newActor = null;
			this.setUpdater(null);
			this.setVisible(false);
			SimulatorFrame.getInstance().setLocationRelativeTo(null);
			SimulatorFrame.getInstance().setVisible(true);
			this.setFieldsValuesEmpty();
		} else {
			ErrorFrame.getInstance()
					.setLabel("Wrong inserted values in fields");
			ErrorFrame.getInstance().setBackFrame("CreateActorFrame");
			ErrorFrame.getInstance().setLocationRelativeTo(null);
			ErrorFrame.getInstance().setVisible(true);
			this.setVisible(false);
		}
	}// GEN-LAST:event_jButton1ActionPerformed

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton2ActionPerformed
		this.setFieldsValuesEmpty();
                SimulatorFrame.getInstance().setActorsList(this.getActorsList());
		this.setVisible(false);
		SimulatorFrame.getInstance().setLocationRelativeTo(null);
		SimulatorFrame.getInstance().setVisible(true);
	}// GEN-LAST:event_jButton2ActionPerformed

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton3ActionPerformed
		this.setVisible(false);
		if (this.newActor == null)
			this.newActor = new Actor("actor"
					+ String.valueOf(this.getActorsList().size()), "actor",
					null, 80, null, 0, 0, null, 0, null, null);
		UpdaterFrame currentUpdater = new UpdaterFrame();
		this.getResourcesList();
		currentUpdater.setResourcesList(this.getResourcesList());
		this.getMainResourcesList().add(newActor);
		currentUpdater.setMainResourcesList(this.getMainResourcesList());
		if (this.getUpdater() != null)
			currentUpdater.setUpdater(this.getUpdater());
		else
			currentUpdater.setUpdater(new Updater());
		currentUpdater.setBackFrameText("CreateActorFrame");
		currentUpdater.setBackFrame(this);
		currentUpdater.setLocationRelativeTo(null);
		currentUpdater.setVisible(true);
		currentUpdater.fillTable();
	}// GEN-LAST:event_jButton3ActionPerformed

	private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jRadioButton1ActionPerformed
		this.jButton3.setEnabled(this.jRadioButton1.isSelected());
	}// GEN-LAST:event_jRadioButton1ActionPerformed

	private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jTextField3ActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jTextField3ActionPerformed

	public void setActorsList(Vector<Actor> actorsList) {
		this.actorsList = actorsList;
	}

	private void setFieldsValuesEmpty() {
		this.jTextField1.setText(null);
		this.jTextField2.setText(null);
		this.jTextField3.setText(null);
		this.jTextField4.setText(null);
		this.clearTable();
                this.setUpdater(new Updater());
	}

	public void setMainResourcesList(Vector<Resource> resourcesPrincipalList) {
		this.resourcesMainList = resourcesPrincipalList;
	}

	public void setResourcesList(Vector<Resource> resourcesList) {
		this.resourcesList = resourcesList;
	}

	public void setSchedulingSystem(SchedulingSystem schedulingSystem) {
		this.currentSchedulingSystem = schedulingSystem;
	}

	public void setUpdater(Updater updater) {
		this.updater = updater;
	}
}
